import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../model/PPShopHomePageModel.dart';

class PPRowImageView extends StatefulWidget {

    PPRowImageView(List<PPShopPlateContent> data, { Key key, this.h = 10, this.onPressAction } ): this.data = data, super(key: key);

    final List<PPShopPlateContent> data;
    final double h;
    final Function onPressAction;

    _PPRowImageViewState createState() => _PPRowImageViewState();

}

class _PPRowImageViewState extends State<PPRowImageView> {

    PPShopPlateContent data;
    double h;

    @override
    void initState() {
        data = widget.data == null || widget.data.length == 0 ? {} : widget.data[0];
        h = widget.h;
        super.initState();
    }

    @override
    void didUpdateWidget(PPRowImageView oldWidget) {
        data = widget.data == null || widget.data.length == 0 ? {} : widget.data[0];
        h = widget.h;
        super.didUpdateWidget(oldWidget);
    }

    @override
    Widget build(BuildContext context) {
        double screenWidth = MediaQuery.of(context).size.width;
        if(data.url == null || data.url.length == 0) {
            return null;
        } else {
            return (Container(
                    margin: EdgeInsets.all(0),
                    padding: EdgeInsets.all(0),
                    width: screenWidth,
                    height: h,
                    child: FlatButton(
                        padding: EdgeInsets.all(0),
                        child: Image.network(data.url, width: screenWidth, height: h, fit: BoxFit.fitWidth),
                        onPressed: (){  _onPressImageItem(data, 0); },
                    )
                )
            );
        }
    }

    _onPressImageItem(PPShopPlateContent data, int index) {
        if(widget.onPressAction != null) {
            widget.onPressAction(data, index);
        }
    }
} 
